package com.tjcd.project.library_base.common.upload.net;

import android.content.Context;
import android.view.View;

import androidx.fragment.app.Fragment;

/**
 * @Author junweiliu
 * @Description 基础Presenter
 * @Version 1.0
 * @CreateDate 2019/12/24
 * @QQ 1007271386
 */
public abstract class BasePresenter<V, M extends BaseModel> {
    /**
     * 上下文
     */
    public Context mContext;
    /**
     * view
     */
    public V mView;
    /**
     * models
     */
    public M[] mModels;
    /**
     * model
     */
    public M mModel;


    /**
     * 绑定
     *
     * @param context
     */
    public void attachView(Context context) {
        this.mContext = context;
        this.mView = (V) context;
        initModel();
    }

    /**
     * 绑定
     *
     * @param view
     * @param context
     */
    public void attachFragment(View view, Fragment context) {
        this.mContext = context.getActivity();
        this.mView = (V) context;
        initModel();
    }

    /**
     * 解绑
     */
    public void detachView() {
        if (null != mModel) {
            mModel.cancelBiz();
        }
        if (null != mModels && mModels.length > 0) { // 销毁连接
            for (M m : mModels) {
                m.cancelBiz();
            }
        }
        if (null != mContext) {
            mContext = null;
        }
        if (null != mView) {
            mView = null;
        }
    }

    protected abstract void initModel();

    /**
     * 设置models
     *
     * @param models
     */
    public void setModels(M... models) {
        mModels = models;
    }

    /**
     * 设置model
     *
     * @param model
     */
    public void setModel(M model) {
        mModel = model;
    }

}
